﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharp.Rest;
using RATSharp.Model;
using System.Threading.Tasks;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;

namespace RATSharpW7.View
{   

    /// <summary>
    /// Favourites screen. Just show all favourites and navigates
    /// to object view is user select any.
    /// </summary>
    public partial class Favourites : PhoneApplicationPage
    {
        bool isNewPageInstance = false;
        public FavouritesViewModel model { get; set; } 
        
        public Favourites()
        {
            InitializeComponent();
            isNewPageInstance = true;
        }

    private void FavouritesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            // get  item from ListBox and store in class var. Store page to navigate to in class var.
            RATSharp.Model.Object selectedItem = (RATSharp.Model.Object)e.AddedItems[0];
            //Uri targetPage = new Uri("/View/ObjectProfile.xaml", UriKind.Relative);



           
            // reset selection of ListBox
            ((ListBox)sender).SelectedIndex = -1;

            // PageTransition.Begin();

            // change  page navigation 
            NavigationService.Navigate("View" , typeof(ObjectProfile), selectedItem);

        }
    }

    private void Tap_LeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        StackPanel rect = sender as StackPanel;
        // Change the size of the Rectangle.
        if (null != rect)
        {
            rect.Opacity = 0.5;
        }
    }
    private void Tap_LeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        StackPanel rect = sender as StackPanel;
        // Reset the dimensions on the Rectangle.
        if (null != rect)
        {
            rect.Opacity = 1.0;

        }
    }
    private void Tap_MouseLeave(object sender, MouseEventArgs e)
    {
        StackPanel rect = sender as StackPanel;
        // Finger moved out of Rectangle before the mouse up event.
        // Reset the dimensions on the Rectangle.


        if (null != rect)
        {
            rect.Opacity = 1.0;

        }
    }

        
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
          if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("FavouritesViewModel"))
                    {
                        model = State["FavouritesViewModel"]
                                           as FavouritesViewModel;
                    }
                    else
                    {
                        model = new FavouritesViewModel();
                    }
                }
                DataContext = model;
            }
            isNewPageInstance = false;
            model.loadFavourites();
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationMode !=
                  System.Windows.Navigation.NavigationMode.Back)
            {
                State["FavouritesViewModel"] = model;
            }
        }

    }


    }